package com.yanftch.stopcoding.ui;

import com.yanftch.stopcoding.common.util.LogUtil;
import com.yanftch.stopcoding.common.util.TextUtils;
import com.yanftch.stopcoding.config.DefaultConfig;
import com.yanftch.stopcoding.manager.NotifyManager;
import org.tukaani.xz.check.Check;

import javax.swing.*;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class PluginSettingPage {
    private static final String TAG = "debug_PluginSettingPage";

    private JComboBox cbImageSource;
    private JComboBox cbTextSource;
    private JTextField tfMinute;
    private JTextField tfDialogTitle;
    private JPanel rootPanel;
    private JCheckBox checkBoxSwitch;

    public PluginSettingPage() {
        init();
    }

    private void init() {
        // 图片源初始化
        initImageSource();
        initTextSource();
        initPeriodMinutes();
        // 添加 ItemListener
        checkBoxSwitch.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    NotifyManager.restart();
                } else {
                    NotifyManager.stop();
                }
            }
        });
    }

    private void initPeriodMinutes() {
        ((AbstractDocument) tfMinute.getDocument()).setDocumentFilter(new NumericDocumentFilter());
    }

    private void initImageSource() {
        DefaultConfig.ImageSourceEnum[] values = DefaultConfig.ImageSourceEnum.values();
        for (DefaultConfig.ImageSourceEnum imageSrcType : values) {
            cbImageSource.addItem(imageSrcType.description);
        }
        cbImageSource.addItemListener(new ImageSrcChangeAction());
    }

    private void initTextSource() {
        DefaultConfig.TextSourceEnum[] values = DefaultConfig.TextSourceEnum.values();
        for (DefaultConfig.TextSourceEnum textSourceEnum : values) {
            cbTextSource.addItem(textSourceEnum.description);
        }
        cbTextSource.addItemListener(new TextSrcChangeAction());
    }

    public void reset() {
        cbImageSource.setSelectedIndex(0);
        cbTextSource.setSelectedIndex(0);
        tfMinute.setText(String.valueOf(DefaultConfig.DEFAULT_PERIOD_MINUTES));
        tfDialogTitle.setText(DefaultConfig.DEFAULT_DIALOG_TITLE);
    }

    public JPanel getMainPanel() {
        return rootPanel;
    }

    public void setPeriodMinutes(int periodMinutes) {
        tfMinute.setText(String.valueOf(periodMinutes));
    }

    public int getPeriodMinutes() {
        try {
            return Integer.parseInt(tfMinute.getText());
        } catch (Exception e) {
            return DefaultConfig.DEFAULT_PERIOD_MINUTES;
        }
    }

    public boolean getSwitchState() {
        return checkBoxSwitch.isSelected();
    }

    public JCheckBox getCheckBoxSwitch() {
        return checkBoxSwitch;
    }

    public String getDialogTitle() {
        return tfDialogTitle.getText();
    }

    public void setDialogTitle(String title) {
        tfDialogTitle.setText(title);
    }

    public String getImageSrcText() {
        if (cbImageSource.getSelectedItem() != null) {
            String string = cbImageSource.getSelectedItem().toString();
            DefaultConfig.ImageSourceEnum type = DefaultConfig.ImageSourceEnum.fromString(string);
            String description = type.description;
            return description;
        }
        return DefaultConfig.DEFAULT_IMAGE_SRC;
    }

    public void setImageSrcText(String imageSrcText) {
        DefaultConfig.ImageSourceEnum type = DefaultConfig.ImageSourceEnum.fromString(imageSrcText);
        cbImageSource.setSelectedItem(type.description);
    }

    public String getTextSrcText() {
        if (cbTextSource.getSelectedItem() != null) {
            String string = cbTextSource.getSelectedItem().toString();
            DefaultConfig.TextSourceEnum type = DefaultConfig.TextSourceEnum.fromString(string);
            String description = type.description;
            return description;
        }
        return DefaultConfig.DEFAULT_TEXT_SRC;
    }

    public void setTextSrcText(String textSrcText) {
        DefaultConfig.TextSourceEnum type = DefaultConfig.TextSourceEnum.fromString(textSrcText);
        cbTextSource.setSelectedItem(type.description);
    }

    /**
     * 图片源下拉框选项改变的监听类
     */
    class ImageSrcChangeAction implements ItemListener {

        @Override
        public void itemStateChanged(ItemEvent e) {
            String desc = e.getItem().toString();
            DefaultConfig.ImageSourceEnum type = DefaultConfig.ImageSourceEnum.fromString(desc);
            LogUtil.d(TAG, "图片源下拉框选项改变，选中的图片源为：" + type);
        }
    }

    /**
     * 文案源下拉框选项改变的监听类
     */
    class TextSrcChangeAction implements ItemListener {

        @Override
        public void itemStateChanged(ItemEvent e) {
            String desc = e.getItem().toString();
            DefaultConfig.TextSourceEnum type = DefaultConfig.TextSourceEnum.fromString(desc);
            LogUtil.d(TAG, "文案源下拉框选项改变，选中的文案源为：" + type);
        }
    }

    /**
     * 时间设置框输入校验
     */
    class NumericDocumentFilter extends DocumentFilter {
        @Override
        public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
            if (!TextUtils.isEmpty(string) && string.matches("\\d*")) {  // 允许的输入是数字
                super.insertString(fb, offset, string, attr);
            }
        }

        @Override
        public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
            if (!TextUtils.isEmpty(text) && text.matches("\\d*")) {  // 允许的输入是数字
                super.replace(fb, offset, length, text, attrs);
            }
        }

        @Override
        public void remove(FilterBypass fb, int offset, int length) throws BadLocationException {
            super.remove(fb, offset, length);
        }
    }
}
